import ColorUISdk from '../../mp-sdk/index'

const sdk = new ColorUISdk({
  env: 'dev',
  version: '1.0.0',
  api: {
    dev: { 
      url: 'http://192.168.134.31:5000'
    }
  }
})

Page({
  data: {
    feedback: null as any,
    loading: true,
    statusList: [
      { name: '审核中', color: '#ff9800' },
      { name: '已派单', color: '#2196f3' },
      { name: '已接单', color: '#4caf50' },
      { name: '修复中', color: '#9c27b0' },
      { name: '已完成', color: '#009688' }
    ]
  },

  onLoad(options: any) {
    if (options.id) {
      this.getFeedbackDetail(options.id)
    } else {
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
    }
  },

  // 获取反馈详情
  async getFeedbackDetail(id: string) {
    try {
      this.setData({ loading: true })
      
      const res = await sdk.request({
        url: `/feedback/detail?id=${id}`,
        method: 'GET'
      })
      
      if (res.code === 0) {
        // 格式化时间
        const feedback = {
          ...res.data,
          createTime: this.formatTime(res.data.createTime),
          updateTime: res.data.updateTime ? this.formatTime(res.data.updateTime) : ''
        }
        
        this.setData({
          feedback,
          loading: false
        })
      } else {
        this.setData({ loading: false })
        wx.showToast({
          title: res.msg || '获取详情失败',
          icon: 'none'
        })
      }
    } catch (error) {
      this.setData({ loading: false })
      wx.showToast({
        title: '网络错误',
        icon: 'none'
      })
    }
  },

  // 格式化时间
  formatTime(timeString: string) {
    const date = new Date(timeString)
    return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
  },

  // 预览图片
  previewImage(e: any) {
    const { index } = e.currentTarget.dataset
    const { feedback } = this.data
    
    wx.previewImage({
      current: feedback.images[index],
      urls: feedback.images
    })
  }
})